Comparison of samples from the 6th Time_point (6_Post-FMT2)
# Find numeric columns
numeric_cols <- sapply(element_gift_TM6, is.numeric)
# Calculate column sums for numeric columns only
col_sums_numeric <- colSums(element_gift_TM6[, numeric_cols])
# Identify numeric columns with sums not equal to zero
nonzero_numeric_cols <- names(col_sums_numeric)[col_sums_numeric != 0]
# Remove numeric columns with sums not equal to zero
filtered_data <- element_gift_TM6[, !numeric_cols | colnames(element_gift_TM6) %in% nonzero_numeric_cols]
significant_elements_TM6 <- filtered_data %>%
pivot_longer(-c(Tube_code,type), names_to = "trait", values_to = "value") %>%
group_by(trait) %>%
summarise(p_value = wilcox.test(value ~ type, exact=FALSE)$p.value) %>%
mutate(p_adjust=p.adjust(p_value, method="BH")) %>%
filter(p_value < 0.05) %>% #take into account that p_value is used and not p_adjust
left_join(.,uniqueGIFT_db[c(1,3)],by = join_by(trait == Code_element))
element_gift_t <- element_gift_TM6 %>%
dplyr::select(-c(type)) %>%
t() %>%
row_to_names(row_number = 1) %>%
as.data.frame() %>%
mutate_if(is.character, as.numeric) %>%
rownames_to_column(., "trait")
element_gift_filt <- subset(element_gift_t, trait %in% significant_elements_TM6$trait) %>%
t() %>%
row_to_names(row_number = 1) %>%
as.data.frame() %>%
mutate_if(is.character, as.numeric) %>%
rownames_to_column(., "Tube_code")%>%
left_join(., sample_metadata_TM6[c(1,7)], by = join_by(Tube_code == Tube_code))
element_gift_filt %>%
dplyr::select(-Tube_code)%>%
group_by(type) %>%
summarise(across(everything(), mean))%>%
t() %>%
row_to_names(row_number = 1) %>%
as.data.frame() %>%
mutate_if(is.character, as.numeric) %>%
rownames_to_column(., "Elements") %>%
left_join(.,uniqueGIFT_db[c(1,3)],by = join_by(Elements == Code_element))
element_gift_names <- element_gift_filt%>%
dplyr::select(-type)%>%
t() %>%
row_to_names(row_number = 1) %>%
as.data.frame() %>%
mutate_if(is.character, as.numeric) %>%
rownames_to_column(., "Elements") %>%
left_join(.,uniqueGIFT_db[c(1,3)],by = join_by(Elements == Code_element))%>%
dplyr::select(-Elements)%>%
dplyr::select(Function, everything())%>%
t()%>%
row_to_names(row_number = 1) %>%
as.data.frame() %>%
mutate_if(is.character, as.numeric) %>%
rownames_to_column(., "Tube_code")%>%
left_join(., sample_metadata_TM6[c(1,7)], by = join_by(Tube_code == Tube_code))
Plot
colNames <- names(element_gift_names)[2:20] #always check names(element_gift_names) first to now were your traits finish
for(i in colNames){
plt <- ggplot(element_gift_names, aes(x=type, y=.data[[i]], color = type)) +
geom_boxplot(alpha = 0.2, outlier.shape = NA, width = 0.3, show.legend = FALSE) +
geom_jitter(width = 0.1, show.legend = TRUE) +
theme_minimal() +
theme(
axis.line = element_line(size = 0.5, linetype = "solid", colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank())
print(plt)
}



















Comparison of samples from the 0 Time_point (0_Wild)
sample_metadata_wild <- sample_metadata%>%
filter(time_point == "0_Wild")
element_gift_wild <- GIFTs_elements_community %>%
as.data.frame() %>%
rownames_to_column(., "Tube_code") %>%
inner_join(., sample_metadata_wild[c(1,3)], by="Tube_code")
# Find numeric columns
numeric_cols <- sapply(element_gift_wild, is.numeric)
# Calculate column sums for numeric columns only
col_sums_numeric <- colSums(element_gift_wild[, numeric_cols])
# Identify numeric columns with sums not equal to zero
nonzero_numeric_cols <- names(col_sums_numeric)[col_sums_numeric != 0]
# Remove numeric columns with sums not equal to zero
filtered_data <- element_gift_wild[, !numeric_cols | colnames(element_gift_wild) %in% nonzero_numeric_cols]
significant_elements_wild <- filtered_data %>%
pivot_longer(-c(Tube_code,species), names_to = "trait", values_to = "value") %>%
group_by(trait) %>%
summarise(p_value = wilcox.test(value ~ species, exact=FALSE)$p.value) %>%
mutate(p_adjust=p.adjust(p_value, method="BH")) %>%
filter(p_adjust < 0.05) %>%
left_join(.,uniqueGIFT_db[c(1,3)],by = join_by(trait == Code_element))
element_gift_t <- element_gift_wild %>%
dplyr::select(-c(species)) %>%
t() %>%
row_to_names(row_number = 1) %>%
as.data.frame() %>%
mutate_if(is.character, as.numeric) %>%
rownames_to_column(., "trait")
element_gift_filt <- subset(element_gift_t, trait %in% significant_elements_wild$trait) %>%
t() %>%
row_to_names(row_number = 1) %>%
as.data.frame() %>%
mutate_if(is.character, as.numeric) %>%
rownames_to_column(., "Tube_code")%>%
left_join(., sample_metadata_wild[c(1,3)], by = join_by(Tube_code == Tube_code))
element_gift_filt %>%
dplyr::select(-Tube_code)%>%
group_by(species) %>%
summarise(across(everything(), mean))%>%
t() %>%
row_to_names(row_number = 1) %>%
as.data.frame() %>%
mutate_if(is.character, as.numeric) %>%
rownames_to_column(., "Elements") %>%
left_join(.,uniqueGIFT_db[c(1,3)],by = join_by(Elements == Code_element))
element_gift_names <- element_gift_filt%>%
dplyr::select(-species)%>%
t() %>%
row_to_names(row_number = 1) %>%
as.data.frame() %>%
mutate_if(is.character, as.numeric) %>%
rownames_to_column(., "Elements") %>%
left_join(.,uniqueGIFT_db[c(1,3)],by = join_by(Elements == Code_element))%>%
dplyr::select(-Elements)%>%
dplyr::select(Function, everything())%>%
t()%>%
row_to_names(row_number = 1) %>%
as.data.frame() %>%
mutate_if(is.character, as.numeric) %>%
rownames_to_column(., "Tube_code")%>%
left_join(., sample_metadata_wild[c(1,3)], by = join_by(Tube_code == Tube_code))
Plot
colNames <- names(element_gift_names)[2:34] #always check names(element_gift_names) first to know were your traits finish
for(i in colNames){
plt <- ggplot(element_gift_names, aes(x=species, y=.data[[i]], color = species)) +
geom_boxplot(alpha = 0.2, outlier.shape = NA, width = 0.3, show.legend = FALSE) +
geom_jitter(width = 0.1, show.legend = TRUE) +
theme_minimal() +
theme(
axis.line = element_line(size = 0.5, linetype = "solid", colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank())
print(plt)
}
































